/**
 * Map object in JavaScript.
 *
 * Note: a "newLoadUserHandler" function needs to be defined outside!
 *
 * By Laurens Van Keer, for the "iGeoTrack" project.
 * http://mumetech.wordpress.com/
 */
function Map(mapElement, lat, lon)
{
	this.mapElement = mapElement;			// element that should contain the google map
	this.mapObject;							// the google map object
	this.showCurrentUserMarker = true;		// default: show the user marker
	this.currentUserMarker;					// the user marker object
	this.currentLatitude = lat;				// latitude of current user
	this.currentLongitude = lon;			// longitude of current user
	this.markers;							// markers representing nearby users
	
	/**
	 * Changes showCurrentUserMarker from true to false and back.
	 */
	this.changeShowCurrentUserMarker = function()
	{
		if(this.showCurrentUserMarker)
			this.showCurrentUserMarker = false;
		else
			this.showCurrentUserMarker = true;
	}
	
	/**
	 * Loads the map.
	 *
	 * @param	user
	 * 			The name of the current user.
	 * @param	users
	 *			An array object containing the nearby users.
	 */
	this.loadMap = function(user, users)
	{
		// init
		var myOptions = {
			zoom: 14,
			center: new google.maps.LatLng(this.currentLatitude, this.currentLongitude),
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		
		if (this.mapObject)
			this.mapObject.setOptions(myOptions);
		else
			this.mapObject = new google.maps.Map(mapElement, myOptions);
		
		// load current user marker
		this.loadCurrentUserMarker(user);
		
		// load the markers for the nearby users
		this.loadUserMarkers(users);
	}
	
	/**
	 * Places a marker for the current user.
	 */
	this.loadCurrentUserMarker = function(user)
	{
		this.currentUserMarker = null; // delete marker
		
		// place marker for the current user (if necessary)
		if (this.showCurrentUserMarker) {
			this.currentUserMarker = new google.maps.Marker({
				position: new google.maps.LatLng(this.currentLatitude, this.currentLongitude), 
				map: this.mapObject, 
				title: "This is you!"
			});
			
			google.maps.event.addListener(this.currentUserMarker, 'click', function() {
				var infoWindow = new google.maps.InfoWindow({
					position: this.currentUserMarker.getPosition(),
					content: "This is you!<br /><b>" + user + "</b>"
				});
				infoWindow.open(this.mapObject, this.currentUserMarker);
			});
		}
	}
	
	/**
	 * Places markers for the nearby users.
	 *
	 * !!!!!!!!!!!!!!!! A newLoadUserHandler needs to be defined !!!!!!!!!!!!!!!!
	 *
	 */
	this.loadUserMarkers = function(users)
	{
		this.markers = null; // delete markers
		
		// place markers for all nearby users
		if (users) {
			this.markers = new Array(users.length);
			for (i in users) {
			
				this.markers[i] = new google.maps.Marker({
					position: new google.maps.LatLng(users[i].Latitude, users[i].Longitude), 
					map: this.mapObject, 
					title: users[i].Name
				});
				
				google.maps.event.addListener(this.markers[i], 'click', newLoadUserHandler(i));
				
			}
		}
	}
}